home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / pcmagwin.zip / BITFONTS.C < prev    next >
Text File  |  1992-12-30  |  10KB  |  284 lines

  1. /*------------------------------------------
  2.    BITFONTS.C -- Displays OS/2 Bitmap Fonts
  3.                  (c) Charles Petzold, 1993
  4.   ------------------------------------------*/
  5.  
  6. #define INCL_WIN
  7. #define INCL_GPI
  8. #include <os2.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "bitfonts.h"
  13.  
  14. #define LCID_FONT   1
  15.  
  16. MRESULT EXPENTRY ClientWndProc (HWND, ULONG, MPARAM, MPARAM) ;
  17.  
  18. int main (void)
  19.      {
  20.      static CHAR  szClientClass [] = "BitFonts" ;
  21.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
  22.                                  FCF_SIZEBORDER    | FCF_MINMAX   |
  23.                                  FCF_SHELLPOSITION | FCF_TASKLIST |
  24.                                  FCF_MENU ;
  25.      HAB          hab ;
  26.      HMQ          hmq ;
  27.      HWND         hwndFrame, hwndClient ;
  28.      QMSG         qmsg ;
  29.  
  30.      hab = WinInitialize (0) ;
  31.      hmq = WinCreateMsgQueue (hab, 0) ;
  32.  
  33.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  34.  
  35.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  36.                                      &flFrameFlags, szClientClass,
  37.                                      "Bitmap Fonts", 0L,
  38.                                      NULLHANDLE, ID_RESOURCE, &hwndClient) ;
  39.  
  40.      while (WinGetMsg (hab, &qmsg, NULLHANDLE, 0, 0))
  41.           WinDispatchMsg (hab, &qmsg) ;
  42.  
  43.      WinDestroyWindow (hwndFrame) ;
  44.      WinDestroyMsgQueue (hmq) ;
  45.      WinTerminate (hab) ;
  46.      return 0 ;
  47.      }
  48.  
  49. LONG GetAllBitmapFonts (HPS hps, PFONTMETRICS * ppfm)
  50.      {
  51.      HDC          hdc ;
  52.      LONG         l, lAllFnt, lBitFnt, xRes, yRes ;
  53.      PFONTMETRICS pfmAll, pfmBit ;
  54.  
  55.                // Find number of fonts
  56.  
  57.      lAllFnt = 0 ;
  58.      lAllFnt = GpiQueryFonts (hps, QF_PUBLIC, NULL, &lAllFnt, 0, NULL) ;
  59.  
  60.      if (lAllFnt ==  0)
  61.           return 0 ;
  62.  
  63.                // Allocate memory for FONTMETRICS structures
  64.  
  65.      pfmAll = (PFONTMETRICS) calloc (lAllFnt, sizeof (FONTMETRICS)) ;
  66.  
  67.      if (pfmAll == NULL)
  68.           return 0 ;
  69.  
  70.      pfmBit = (PFONTMETRICS) calloc (lAllFnt, sizeof (FONTMETRICS)) ;
  71.  
  72.      if (pfmBit == NULL)
  73.           {
  74.           free (pfmAll) ;
  75.           return 0 ;
  76.           }
  77.                // Get all fonts
  78.  
  79.      GpiQueryFonts (hps, QF_PUBLIC, NULL, &lAllFnt,
  80.                          sizeof (FONTMETRICS), pfmAll) ;
  81.  
  82.                // Determine font resolution
  83.  
  84.      hdc = GpiQueryDevice (hps) ;
  85.  
  86.      DevQueryCaps (hdc, CAPS_HORIZONTAL_FONT_RES, 1, &xRes) ;
  87.      DevQueryCaps (hdc, CAPS_VERTICAL_FONT_RES,   1, &yRes) ;
  88.  
  89.                // Get all the bitmap fonts
  90.  
  91.      lBitFnt = 0 ;
  92.  
  93.      for (l = 0 ; l < lAllFnt ; l++)
  94.           {
  95.           if (!(pfmAll[l].fsDefn & FM_DEFN_OUTLINE) &&
  96.                 pfmAll[l].sXDeviceRes == xRes       &&
  97.                 pfmAll[l].sYDeviceRes == yRes)
  98.                {
  99.                pfmBit [lBitFnt ++] = pfmAll [l] ;
  100.                }
  101.           }
  102.                // Clean up
  103.  
  104.      free (pfmAll) ;
  105.      pfmBit = (PFONTMETRICS) realloc (pfmBit, lBitFnt * sizeof (FONTMETRICS)) ;
  106.  
  107.      * ppfm = pfmBit ;
  108.  
  109.      return lBitFnt ;
  110.      }
  111.  
  112. MRESULT EXPENTRY ClientWndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  113.      {
  114.      static BOOL         fItalic, fUnder, fOutline, fStrike, fBold,
  115.                          fBaseline, fTextBox ;
  116.      static FATTRS       fat ;
  117.      static HWND         hwndMenu ;
  118.      static LONG         lFonts ;
  119.      static PFONTMETRICS pfm ;
  120.      static SHORT        cyClient ;
  121.      CHAR                szBuffer [FACESIZE + 32] ;
  122.      FONTMETRICS         fm ;
  123.      HPS                 hps ;
  124.      int                 i ;
  125.      LONG                l ;
  126.      POINTL              ptl, aptl [TXTBOX_COUNT] ;
  127.  
  128.      switch (msg)
  129.       {
  130.           case WM_CREATE:
  131.                          // Get the array of FONTMETRICS structures
  132.  
  133.                hps = WinGetPS (hwnd) ;
  134.                GpiQueryFontAction (hps, QFA_PUBLIC) ;
  135.                lFonts = GetAllBitmapFonts (hps, & pfm) ;
  136.                WinReleasePS (hps) ;
  137.  
  138.                          // Get menu window handle
  139.  
  140.                hwndMenu = WinWindowFromID (WinQueryWindow (hwnd, QW_PARENT),
  141.                                            FID_MENU) ;
  142.                return 0 ;
  143.  
  144.           case WM_SIZE:
  145.                cyClient = HIUSHORT (mp2) ;
  146.                return 0 ;
  147.  
  148.           case WM_COMMAND:
  149.                switch (COMMANDMSG(&msg)->cmd)
  150.                     {
  151.                     case IDM_ITALIC:      fItalic   = ! fItalic   ;  break ;
  152.                     case IDM_UNDERSCORE:  fUnder    = ! fUnder    ;  break ;
  153.                     case IDM_OUTLINE:     fOutline  = ! fOutline  ;  break ;
  154.                     case IDM_STRIKEOUT:   fStrike   = ! fStrike   ;  break ;
  155.                     case IDM_BOLD:        fBold     = ! fBold     ;  break ;
  156.                     case IDM_BASELINE:    fBaseline = ! fBaseline ;  break ;
  157.                     case IDM_TEXTBOX:     fTextBox  = ! fTextBox  ;  break ;
  158.  
  159.                     default:  return 0 ;
  160.                     }
  161.  
  162.                WinCheckMenuItem (hwndMenu, COMMANDMSG(&msg)->cmd,
  163.                     ! WinIsMenuItemChecked (hwndMenu, COMMANDMSG(&msg)->cmd)) ;
  164.  
  165.                WinInvalidateRect (hwnd, NULL, TRUE) ;
  166.                return 0 ;
  167.  
  168.           case WM_PAINT:
  169.                hps = WinBeginPaint (hwnd, NULLHANDLE, NULL) ;
  170.  
  171.                GpiErase (hps) ;
  172.  
  173.                     // Get new fonts if they've changed
  174.  
  175.                if (QFA_PUBLIC & GpiQueryFontAction (hps, QFA_PUBLIC))
  176.                     {
  177.                     free (pfm) ;
  178.                     lFonts = GetAllBitmapFonts (hps, & pfm) ;
  179.                     }
  180.  
  181.                     // Set POINTL structure to upper left corner of client
  182.  
  183.                ptl.x = 0 ;
  184.                ptl.y = cyClient ;
  185.  
  186.                     // Loop through all the bitmap fonts
  187.  
  188.                for (l = 0 ; l < lFonts ; l++)
  189.                     {
  190.                          // Define the FATTRS structure
  191.  
  192.                     fat.usRecordLength  = sizeof (FATTRS) ;
  193.  
  194.                     fat.fsSelection = (fItalic  ? FATTR_SEL_ITALIC     : 0) |
  195.                                       (fUnder   ? FATTR_SEL_UNDERSCORE : 0) |
  196.                                       (fOutline ? FATTR_SEL_OUTLINE    : 0) |
  197.                                       (fStrike  ? FATTR_SEL_STRIKEOUT  : 0) |
  198.                                       (fBold    ? FATTR_SEL_BOLD       : 0) ;
  199.  
  200.                     fat.lMatch          = 0 ;
  201.  
  202.                     strcpy (fat.szFacename, pfm[l].szFacename) ;
  203.  
  204.                     fat.idRegistry      = pfm[l].idRegistry ;
  205.                     fat.usCodePage      = pfm[l].usCodePage ;
  206.                     fat.lMaxBaselineExt = pfm[l].lMaxBaselineExt ;
  207.                     fat.lAveCharWidth   = pfm[l].lAveCharWidth ;
  208.                     fat.fsType          = 0 ;
  209.                     fat.fsFontUse       = 0 ;
  210.  
  211.                          // Create the logical font and select it
  212.  
  213.                     GpiCreateLogFont (hps, NULL, LCID_FONT, &fat) ;
  214.                     GpiSetCharSet (hps, LCID_FONT) ;
  215.  
  216.                          // Query the font metrics of the current font
  217.  
  218.                     GpiQueryFontMetrics (hps, sizeof (FONTMETRICS), &fm) ;
  219.  
  220.                          // Set up a text string to display
  221.  
  222.                     sprintf (szBuffer, " \xDB %s - %d points \xDB ",
  223.                              fm.szFacename, fm.sNominalPointSize / 10) ;
  224.  
  225.                          // Drop POINTL structure to baseline of font
  226.  
  227.                     ptl.y -= fm.lMaxAscender ;
  228.  
  229.                          // Display the character string
  230.  
  231.                     GpiCharStringAt (hps, &ptl, strlen (szBuffer), szBuffer) ;
  232.  
  233.                          // Adjust text box points for left baseline
  234.  
  235.                     GpiQueryTextBox (hps, strlen (szBuffer), szBuffer,
  236.                                      TXTBOX_COUNT, aptl) ;
  237.  
  238.                     for (i = 0 ; i < TXTBOX_COUNT ; i++)
  239.                          {
  240.                          aptl[i].x += ptl.x ;
  241.                          aptl[i].y += ptl.y ;
  242.                          }
  243.  
  244.                          // Possibly display the baseline
  245.  
  246.                     if (fBaseline)
  247.                          {
  248.                          GpiMove (hps, &ptl) ;
  249.                          GpiLine (hps, aptl + TXTBOX_CONCAT) ;
  250.                          }
  251.  
  252.                          // Possibly display the character box
  253.  
  254.                     if (fTextBox)
  255.                          {
  256.                          GpiMove (hps, aptl + TXTBOX_TOPLEFT) ;
  257.                          GpiLine (hps, aptl + TXTBOX_TOPRIGHT) ;
  258.                          GpiLine (hps, aptl + TXTBOX_BOTTOMRIGHT) ;
  259.                          GpiLine (hps, aptl + TXTBOX_BOTTOMLEFT) ;
  260.                          GpiLine (hps, aptl + TXTBOX_TOPLEFT) ;
  261.                          }
  262.  
  263.                          // Drop POINTL structure down to bottom of text
  264.  
  265.                     ptl.y -= fm.lMaxDescender ;
  266.  
  267.                          // Select the default font; delete the logical font
  268.  
  269.                     GpiSetCharSet (hps, LCID_DEFAULT) ;
  270.                     GpiDeleteSetId (hps, LCID_FONT) ;
  271.                     }
  272.  
  273.                WinEndPaint (hps) ;
  274.                return 0 ;
  275.  
  276.           case WM_DESTROY:
  277.                if (lFonts > 0)
  278.                     free (pfm) ;
  279.  
  280.                return 0 ;
  281.           }
  282.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  283.      }
  284.